package cn.glutnn.controller;


import cn.glutnn.entity.Question;
import cn.glutnn.entity.Result;
import cn.glutnn.service.QuestionService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * (Question)表控制层
 *
 * @author msh
 * @since 2024-10-25 09:36:08
 */
@RestController
@RequestMapping("/question")
@Tag(name = "问答管理模块")
public class QuestionController {
    /**
     * 服务对象
     */
    @Autowired
    private QuestionService questionService;

    //分页查询所有问答findAllQues
    @Operation(summary = "分页查询所有问答")
    @GetMapping("/findAllQues/{page}")
    public Result<Page<Question>> findAllQues(
            @PathVariable Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        return new Result<>(0, "查询成功", questionService.findAllQues(page, limit));
    }

    //条件查询问答findPageQues
    @Operation(summary = "条件查询问答")
    @GetMapping("/findPageQues/{key}/{page}")
    public Result<Page<Question>> findPageQues(
            @PathVariable Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            @PathVariable(value = "key") String key) {
        return new Result<>(0, "查询成功", questionService.findPageQues(page, limit, key));
    }

    //根据ID查询，查询问答SelectById
    @Operation(summary = "根据ID查询问答")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        return new Result(0, "查询成功", questionService.getById(id));//通过mybatisPlus的getById方法查询
    }

    //添加问答Add
    @Operation(summary = "添加问答")
    @PostMapping("/add")
    public Result add(@RequestBody Question question) {
        Map o = ThreadLocalUtil.get();
        String username = (String) o.get("username");
        question.setQuestioner(username);
        return new Result(0, "添加成功", questionService.save(question));
    }

    //专家问答管理
    @Operation(summary = "专家问答管理")
    // findAllQuesByKind
    @GetMapping("/expertFindAllQues/{kind}")
    public Result<List<Question>> findAllQuesByKind(@PathVariable("kind") String kind) {
        return new Result<List<Question>>(0, "查询成功", questionService.findAllQuesByKind(kind));
    }

    @Operation(summary = "根据ID修改问答")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Question question) {
        return new Result(0, "修改成功", questionService.updateById(question));
    }

    @Operation(summary = "根据ID删除问答")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        return new Result(0, "删除成功", questionService.removeById(id));
    }


}

